import redis as redis


class RedisUtils:

    # 构造方法构造
    def __init__(self):
        # 构建链接池
        self.pool = redis.ConnectionPool(
            host='localhost',
            port=6379,
            password='',
            max_connections=1024
        )
        # 构建链接对象
        self.conn = redis.Redis(connection_pool=self.pool)

    def hexists(self, name, key):
        return self.conn.hexists(name, key)

    def hget(self, name, key):
        return self.hget(name, key)

    def getset(self, name, value):
        return self.conn.getset(name, value)

    def hdel(self, name, *keys):
        return self.conn.hdel(name, *keys)

    def hgetall(self, name):
        return self.conn.hgetall(name)

    def hkeys(self, name):
        return self.conn.hkeys(name)

    def hlen(self, name):
        return self.conn.hlen(name)

    # Set key to value within hash name Returns 1 if HSET created a new field, otherwise 0
    def hset(self, name, key, value):
        return self.conn.hset(name, key, value)

    def setex(self, name, time, value):
        return self.conn.setex(name, time, value)

    def get(self, name):
        return self.conn.get(name)

    def exists(self, name):
        return self.conn.exists(name)

    def set(self, name, value):
        return self.conn.set(name, value)

    # 操作lua脚本
    def lua(self, lua):
        self.conn.register_script(lua)

    #  关闭链接
    def close(self):
        self.conn.close()
